import axios from "axios";
import Cookies from 'js-cookie'
import { showToast } from "vant";
import { decryptByDES, encryptByDES } from "../utils/decryptByDES";


let http = axios.create({
  // import.meta.env.VITE_BASE_URL +
  baseURL: "/api",
  timeout: 30000,
});

// const encUrl = ['/report/cockpit/continueInfoQuery', '/report/cockpit/continueCompanyQuery', '/report/cockpit/continueSaleQuery', '/report/cockpit/actuPremQuery']
// 拦截器的添加
http.interceptors.request.use(
  (config) => {
    config.headers.Authorization = sessionStorage.getItem('jxrs_pc_token')
    // loadingInstance = showToast("加载中");
    // if (encUrl.includes(config.url)) {
    console.log('data', config.data);
    config.data = { data: encryptByDES(JSON.stringify(config.data), 'GLASXCWEAKEY') }
    // console.log('data', config.data);

    // }
    return config;
  },
  (err) => {
    // loadingInstance?.close();
    showToast("网络异常");
    return Promise.reject(err);
  }
);

//响应拦截器
http.interceptors.response.use((res) => {
  // if (encUrl.includes(res.config.url)) {
  res.data.data = res.data.data ? JSON.parse(decryptByDES(res.data.data, 'GLASXCWEAKEY')) : res.data.data

  // console.log('res.data', res.config.url, res.data.data);

  // }
  let code = res.data.code
  if (code == 200) return res.data;
  else if (import.meta.env.VITE_APP_ENV != 'development' && (code == 401 || res.data.msg == '登录状态已过期')) {
    showToast(res.data.msg || '登录失效');
    // Cookies.set('Admin-Token', '', { expires: 0 });

    location.href = '/h5';
  } else {
    showToast(res.data.msg);
    return Promise.reject(res.data);
  }
}, (err) => {
  showToast("请求失败");
  return Promise.reject(err);
}
);
export default http;